<?php

namespace App\Http\Controllers\Cruise\FeishuMini;

use App\Enums\Common\CommonEnum;
use App\Http\Controllers\BaseController;
use App\Http\Middleware\OpenShop\UserPermissionAuth;
use App\Models\Cruise\CruiseTemplateModel;
use App\Service\Cruise\Check\CruiseCheckDetailService;
use App\Service\OpenShop\Feishu\PermissionService;
use App\Service\OpenShop\SiteLocation\AuthService;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;

class CheckDetailController extends BaseController
{

    /**
     * 稽核报告列表
     * @param Request $request
     * @return JsonResponse
     * @throws ContainerExceptionInterface
     * @throws NotFoundExceptionInterface|GuzzleException
     */
    public function reportList(Request $request): JsonResponse
    {
        //加盟商姓名  门店code
        $shopCode        = '';
        $name            = '';
        $search          = $request->get('search', '');
        $status          = $request->get('status', '');
        $type            = $request->get('type', '');
        $level           = $request->get('level', '');
        $cityCode        = $request->get('cityCode', '');
        $pageSize        = $request->get('pageSize', 10);
        $pageNum         = $request->get('pageNum', 1);
        $templateType    = $request->get('templateType', CruiseTemplateModel::TYPE_3);
        //通用模块判断
        $category = $request->header('category', 0);    //category = 5 工厂稽核
        if ($category != 0) {
            $templateType = $category;
        }
        $orderPermission = (new PermissionService())->dataCheckPermission(PermissionService::CRUISE_CHECK_TASK_LISTS, [], UserPermissionAuth::MODULE_CRUISE, PermissionService::ORIGIN_TASK, $templateType);
        if (mb_strlen($search)) {
            if (is_numeric($search)) {
                $shopCode = $search;
            } else {
                $name = $search;
            }
        }

        $paramArr = $request->toArray();

        $data = CruiseCheckDetailService::getCheckDetailList($orderPermission, $pageSize, $pageNum, $shopCode, $name, $status, $type, $level, $cityCode, $templateType, 0, $paramArr);
        return $this->success($data);
    }

    /**
     * 状态统计 （未使用）
     * @param Request $request
     * @return JsonResponse
     */
    public function reportCount(Request $request): JsonResponse
    {
        $authInfo = AuthService::getRmsUserInfo($request);
        $templateType    = $request->get('templateType', CruiseTemplateModel::TYPE_3);
        //通用模块判断
        $category = $request->header('category', 0);    //category = 5 工厂稽核
        if ($category != 0) {
            $templateType = $category;
        }
        return $this->success(CruiseCheckDetailService::count($authInfo, $templateType));
    }

    /**
     * 稽核报告详情
     * @param Request $request
     * @param $id
     * @return JsonResponse
     */
    public function detail(Request $request, $id): JsonResponse
    {
        $authInfo = AuthService::getRmsUserInfo($request);
        return $this->success(CruiseCheckDetailService::detail($id, $authInfo, CommonEnum::PLATFORM_FS_MINI));
    }
}
